<?php

class Products_Model extends Model {

	function Products_Model()
	{
		parent::Model();
	}
	
	//todo: optimize
	function add($newproduct, $categories) {
		
		$this->db->insert('items', $newproduct);
		
		//get the id
		$this->db->select('id');
		$this->db->where('title', $newproduct['title']);
		$this->db->where('upc', $newproduct['upc']);
		$query = $this->db->get('items');
		$results = $query->result();
		$id = $results[0]->id;
		
		//category
		foreach($categories as $category) {
			$this->db->insert('item2cat', array('itemid' => $id, 'catid' => $category));
		}
		
		return $id;
	}

	//todo: optimize
	function get_item($itemid) {
		
		$query = $this->db->query('SELECT * FROM items WHERE id = ' . $itemid);
		$results = $query->result();
		
		$item->id = $results[0]->id;
		$item->title = $results[0]->title;
		$item->description = $results[0]->description;
		$item->image = $results[0]->image;
		$item->price = $results[0]->price;
		$item->date = $results[0]->date;
		$item->invisible = $results[0]->invisible;
		$item->upc = $results[0]->upc;
		$item->sku = $results[0]->sku;
		
		//can be many different categories
		//get a list of the catid's
		$item->categories = $this->Categories_Model->get_my_categories($itemid);
		
		
		return $item;
	}
	
	//todo: optimize
	function get_title($itemid) {
		$this->db->where('id', $itemid);
		$query = $this->db->get('items');
		$results = $query->result();
		return $results[0]->title;
	}
	
	//todo: optimize
	function set_item($item) {
		//validate
		if ($item['title'] == '' || $item['description'] == '' || $item['price'] == '' || $item['image'] == '') {
			return false;
		}
		//is this a new item?
		if ($item['new'] == "TRUE") {
			//create a blank entry in the items table
			$this->new_item();
		}
		
		//we have to deal with the checkboxes for categories
		
		//empty categories
		$query = $this->db->query('DELETE FROM post2cat WHERE itemid = ' . $item['id']);
		//$result = $query->result();
		
		//put categories back in, from the checkboxes
		if (isset($item['cat'])) {
			foreach ($item['cat'] as $cat) {
				//put this category into the db
				$newrow['catid'] = $cat;
				$newrow['itemid'] = $item['id'];
				$this->db->insert('post2cat', $newrow);
			}
		}
		
		//we want item2 = item except without ['cat'] or ['new']
		$item2['id'] 			= 	$item['id'];
		$item2['title'] 		= 	$item['title'];
		$item2['price'] 		= 	$item['price'];
		$item2['description'] 	= 	$item['description'];
		$item2['image'] 		= 	$item['image'];
		
		
		
		$this->db->where('id', $item2['id']);
		$this->db->update('items', $item2);
		
		return true;
		
	}
	
	//todo: optimize
	function get_items($offset = 0, $category = '') {
		if ($category != '') {
			//just items from the specified category
			
			$query = $this->db->query('SELECT itemid FROM post2cat WHERE catid = "' . $category . '" ORDER BY date LIMIT ' . $offset . ', 10');
			
			$results = $query->result();
			
			$i = 0;
			
			foreach ($results as $row) {
				
				$i ++;
				
				$query = $this->db->query('SELECT * FROM items WHERE id=' . $row->itemid);
				$results2 = $query->result();
				$catid = $category; //array
				$catname = $this->get_catname($catid); //array
				$items[$i]->id = $results2[0]->id;
				$items[$i]->title = $results2[0]->title;
				$items[$i]->description = $results2[0]->description;
				$items[$i]->catid = $catid;
				$items[$i]->category = $catname;
				$items[$i]->image = $results2[0]->image;
				$items[$i]->price = $results2[0]->price;
				$items[$i]->date = $results2[0]->date;
				$items[$i]->invisible = $results2[0]->invisible;
			}
			
		} else { //get everything from the items table
			$this->db->order_by("date", "desc");
			$query = $this->db->get('items', 10, $offset);
		
			//now put together an object $items (well, an array of $items)
			//that has everything the database has, except
			//put the name of the category in as well as the category id#.
			$i = 0;
			foreach ($query->result() as $row) {
				$i ++;
				
				//for each row in the items table
				//get the category name
				
				$catid = $this->Categories_Model->get_category($row->id);
				
				$catname = $this->Categories_Model->get_catname($catid);
				
				$items[$i]->id = $row->id;
				$items[$i]->title = $row->title;
				$items[$i]->description = $row->description;
				$items[$i]->catid = $catid;
				$items[$i]->category = $catname;
				$items[$i]->image = $row->image;
				$items[$i]->price = $row->price;
				$items[$i]->date = $row->date;
				$items[$i]->invisible = $row->invisible;
			} 
		}
		
		return $items;
		
	}
	
	//todo: optimize
	function number_items() {
		$query = $this->db->count_all_results('items');
		
		return $query;
	}
	
	
	//todo: optimize
	function delete_item($items) {
		
		
		//get id of each item
		foreach($items as $item) {
			//get the id
			$this->db->select('id');
			$this->db->where('title', $item);
			$query = $this->db->get('items');
			$results = $query->result();
			$this->db->or_where('itemid', $results[0]->id);
		}
		
		$this->db->delete('item2cat');
		
		foreach ($items as $item) {
			$this->db->or_where('title', $item);
		}
		$this->db->delete('items');
		return true;
	}
	
	//todo: optimize
	function get_oldest_year() {
		$query = $this->db->query('SELECT MIN(date) FROM items');
		$results = $query->result();
		//get the year from it
		foreach ($results[0] as $r) {
			//that query make a weird unusable variable name in the object. this is a hack
			//to get at the date
			$oldestyear = substr($r,0,4);
			break;
		}
		return $oldestyear;
	}
	
	function search($terms, $status, $month, $category) {
		if (!empty($terms)) {
			$this->db->like('title', "%$terms%");
			$this->db->or_like('description', "%$terms%");
			$this->db->or_like('upc', "%$terms%");
			$this->db->or_like('sku', "%$terms%");
		}
		
		if ($status != 'any') {
			if ($status == 'visible')
				$this->db->where('invisible', 0);
			else
				$this->db->where('invisible', 1);
		}
		
		$query = $this->db->get('items');
		return $query->result();
		
		if (isset($month)) {
			//convert to date
		}
		
		if (isset($category)) {
			//get the results and then narrow it down by
			//table item2cat
			//return the results
		} else {
			//return the results
		}
	}
	


	
}
?>